package com.tongzhou.tongzhou.quot.action;

import java.io.File;
import java.util.List;
import java.util.TreeMap;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;

import com.opensymphony.xwork2.ActionSupport;
import com.tongzhou.db.exception.DBException;
import com.tongzhou.tongzhou.quot.dao.IPaperNumberPriceDAO;
import com.tongzhou.tongzhou.quot.model.PaperNumberPrice;
import com.tongzhou.util.FileUtil;

public class PaperNumberPriceAction extends ActionSupport{
	private int id;
	private List<PaperNumberPrice> result=null;
	
	private IPaperNumberPriceDAO dao=null;
	private String create_time;
	private File quoturl ;
	/**
	 * 加芯&中隔列表界面
	 *@author: 彭定雄
	 *@return 
	 *@throws DBException
	 */
	public String listPaperNumberPrice() throws DBException{
	    result = dao.getPaperNumberPriceList();
	    if(result.size()>0){
	    	PaperNumberPrice p=result.get(0);
		    create_time=p.getCreate_time();
	    }
		return "listPaperNumberPrice";
	}
	
	public String listPaperNumber() throws DBException{
		 result = dao.getPaperNumberPriceList();
		return "listPaperNumber";
	}
	
	/**
	 * 新增加芯&中隔
	 *@author: 彭定雄
	 *@return
	 *@throws DBException
	 */
	public String toAddPaperNumberPrice() throws DBException{
		return "addPaperNumberPrice";
	}
	
	public String addPaperNumberPrice() throws DBException{
		dao.deletePaperNumberPrice();
		uploadClxxFile();
		listPaperNumberPrice();
		return "listPaperNumberPrice";
	}
	
	/**
	 * 解析excel
	 */
	public void uploadClxxFile(){
		try {
			if(quoturl!=null){
				HSSFSheet sheet=FileUtil.readExcel(quoturl);
				uploadClxxApp(sheet);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ;
		}
	}
	
	public void uploadClxxApp(HSSFSheet sheet) throws Exception {
		int FirstRowNum = sheet.getFirstRowNum();
		int LastRowNum = sheet.getLastRowNum();
		try {
			int j=0;
			TreeMap<Integer,String> map=new TreeMap<Integer,String>();
			for (int i = FirstRowNum+1; i <= LastRowNum; i++) {
				PaperNumberPrice paperNumberPrice=new PaperNumberPrice();
				HSSFRow row = sheet.getRow(i);
				HSSFCell cell1 = row.getCell(0);
				String number = FileUtil.getHSSFCellValue(cell1);
				paperNumberPrice.setNumber(number);
				
				HSSFCell cell2 = row.getCell(1);
				String price1 = FileUtil.getHSSFCellValue(cell2);
				paperNumberPrice.setPrice1(price1);
				
				HSSFCell cell3 = row.getCell(2);
				String price2 = FileUtil.getHSSFCellValue(cell3);
				paperNumberPrice.setPrice2(price2);
				dao.addPaperNumberPrice(paperNumberPrice);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("插入数据时发生异常");
		}
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public List<PaperNumberPrice> getResult() {
		return result;
	}

	public void setResult(List<PaperNumberPrice> result) {
		this.result = result;
	}

	public IPaperNumberPriceDAO getDao() {
		return dao;
	}

	public void setDao(IPaperNumberPriceDAO dao) {
		this.dao = dao;
	}

	public String getCreate_time() {
		return create_time;
	}

	public void setCreate_time(String createTime) {
		create_time = createTime;
	}

	public File getQuoturl() {
		return quoturl;
	}

	public void setQuoturl(File quoturl) {
		this.quoturl = quoturl;
	}
}
